﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Personal : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Server.Transfer("~/Navigate.aspx"); -> Chạy nhanh
        //Redirect -> Chậm
        //Session["Personal"] = "Personal_Value";
        //Application["AppPersonal"] = "AppPersonal_Value";
        // Init day
        for (int i = 0; i <= 31; i++)
        {
            ListItem item = new ListItem();
            item.Text = i.ToString();
            item.Value = i.ToString();
            dayDropList.Items.Add(item);
        }
        Trace.Warn("CheckDropList", dayDropList.Items.Count.ToString());
        // Init month
        for (int i = 0; i <= 12; i++)
        {
            ListItem item = new ListItem();
            item.Text = i.ToString();
            item.Value = i.ToString();
            monthDropList.Items.Add(item);
        }

        // Init year
        for (int i = 1975; i <= 2012; i++)
        {
            ListItem item = new ListItem();
            item.Text = i.ToString();
            item.Value = i.ToString();
            yearDropList.Items.Add(item);
        }

        InitCountry();
        InitCity();

    }

    private void InitBirthday()
    {
        ViewState["Test"] = "Test";
        Session["Test"] = "Test";
        Server.MapPath("");
        /* Các cách lưu dữ liệu trong trang web
         * QueryString
         * Application
         * Cookies
         * Session
         * ViewState
         */
    }
    private void InitCountry()
    {
        // Cách 1
        DataTable countryTable = new DataTable();

        SqlConnection sqlConnection = new SqlConnection("Server=DELL-N5110;Database=Northwind;Trusted_Connection=True;");
        sqlConnection.Open();

        SqlCommand sqlCommand = new SqlCommand("Select * from Country", sqlConnection);
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
        sqlDataAdapter.Fill(countryTable);


        for (int i = 0; i < countryTable.Rows.Count; i++)
        {
            ListItem item = new ListItem();
            item.Text = countryTable.Rows[i]["CountryName"].ToString();
            item.Value = countryTable.Rows[i]["CountryId"].ToString();
            quocGiaDropList.Items.Add(item);
        }
        quocGiaDropList.SelectedIndex = 0;

        // Cách 2, đang có SqlDataReader
        // SqlConnection sqlConnection = new SqlConnection("Data Source=TIEN-HP; Initial Catalog=Nortwind; user id= sa; password=hungtien;");
        // sqlConnection.Open();

        // SqlCommand sqlCommand = new SqlCommand("Select * from Country");
        // SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
        // SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

        //  while(sqlDataReader.Read())
        //  {
        //      ListItem item = new ListItem();
        //      item.Text = sqlDataReader["CountryName"].ToString();
        //      item.Value = sqlDataReader["CountryId"].ToString();
        //      quocGiaDropList.Items.Add(item);
        // }

    }
    private void InitCity()
    {

        // Cách 2, đang có SqlDataReader
        SqlConnection sqlConnection = new SqlConnection("Server=DELL-N5110;Database=Northwind;Trusted_Connection=True;");
        sqlConnection.Open();

        SqlCommand sqlCommand = new SqlCommand("Select * from City where CountryId=@CountryId", sqlConnection);
        SqlParameter sqlParameter = new SqlParameter("@CountryId", SqlDbType.Int);
        sqlParameter.Value = quocGiaDropList.SelectedValue;
        sqlCommand.Parameters.Add(sqlParameter);



        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

        while (sqlDataReader.Read())
        {
            ListItem item = new ListItem();
            item.Text = sqlDataReader["CityName"].ToString();
            item.Value = sqlDataReader["CityId"].ToString();
            thanhPhoDropList.Items.Add(item);
        }
    }

    protected void capNhatButton_Click(object sender, EventArgs e)
    {
        // Insert into Table(Col2, Col1) VALUES (birthDay)
    }
    protected void upLoadButton_Click(object sender, EventArgs e)
    {

        if (FileUpload1.HasFile)
        {
            string path = "~/Images/" + Guid.NewGuid() + System.IO.Path.GetExtension(FileUpload1.FileName);
            FileUpload1.SaveAs(Server.MapPath(path));
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            DateTime birthDay = new DateTime(int.Parse(yearDropList.SelectedValue),
               int.Parse(monthDropList.SelectedValue), int.Parse(dayDropList.SelectedValue));

            args.IsValid = true;
        }
        // ArgumentOutOfRangeException kế thừa từ Exception
        catch (ArgumentOutOfRangeException ex1)
        {
            //Response.Write(ex1.Message + ", " + ex1.ActualValue);
            args.IsValid = false;
        }
        catch (Exception ex)
        {
            args.IsValid = false;
        }

    }
    protected void hoTenText_TextChanged(object sender, EventArgs e)
    {
        //Response.Write("Text Changed");
    }
}